package com.jiang.demo2.control;

import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.jiang.demo2.bean.Result;
import com.jiang.demo2.bean.User;
import com.jiang.demo2.config.ACMConfig;
import com.jiang.demo2.service.CarService;
import com.jiang.demo2.service.UserInfoService;
import com.jiang.demo2.service.UserService;
import com.jiang.demo2.table.Car;
import com.jiang.demo2.table.UserInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

@Slf4j
@Api(tags = "用户DEMO", protocols = "HTTP")
@RestController //标识一个控制器 接收用户请求,RestController=Controller+ResponseBody
@RequestMapping(value = "api/", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class TestController extends BaseControl{

	private static final Logger logger = LoggerFactory.getLogger(TestController.class);

	@Autowired()
	private UserService userService1;

	@Autowired
	private ACMConfig config;

	@Autowired
	private UserInfoService userInfoService;

	@Autowired
	private CarService carService;

	@RequestMapping(value="/acm",method = RequestMethod.GET)//HTTP请求
	@ApiOperation(value="获取acm详细信息", notes="获取acm详细信息")
	public String test() {
		//System.out.println("TestController>>>test>>>is running....return a string");
		logger.info("TestController>>>test>>>is running....return a string");
		logger.error("threadPoolSize"+threadPoolSize + "hello acm --- demo:" + config.demo +  "demo2:" + config.demo2 );
		log.error("@Slf4j");
		return "threadPoolSize"+threadPoolSize + "hello acm --- demo:" + config.demo +  "demo2:" + config.demo2 ;//返回一个视图（HTML页面）
	}

	@NacosValue(value = "${threadPoolSize:3}", autoRefreshed = true)
	private int threadPoolSize;

	@ApiOperation(value="获取用户详细信息", notes="根据code获取用户详细信息")
	@ApiImplicitParam(name = "code", value = "用户code", required = true, dataType = "Integer", paramType = "path")
	@RequestMapping(value="/user11",method = RequestMethod.GET)
	public Result<User> queryUser(@RequestParam String code) {

		if(code!=null){
			return Result.succuse(userService1.getUser(code));
		}else {
			return Result.succuse(userService1.getUser("1"));
		}
	}


	@RequestMapping(value="/queryUser",method = RequestMethod.GET)
	public Result<UserInfo> queryUserInfo(@RequestParam String code){
		UserInfo info = userInfoService.getUserByCode(code);

		if(info!=null){
			Car car = carService.getCarByCode(info.getUsercode());
			info.setCar(car);
			return Result.succuse(info);
		}else {
			return Result.fail(22,"用户不存在");
		}
	}



	@RequestMapping(value="/queryCar",method = RequestMethod.GET)
	public Result<Car> queryCar(@RequestParam int code){
		Car car = carService.getCarByCode(code);

		if(car!=null){
			return Result.succuse(car);
		}else {
			return Result.fail(22,"车不存在");
		}
	}




}

